<?php
namespace app\wap\controller;


use think\Config;
use think\Request;
use app\wap\model\Member;
use expand\Yunst;
use app\wap\model\Mobilelog;
use think\Session;

class Security extends Common
{
    public $cModel;
    public $bizUserId;
    public function _initialize(Request $request){
        parent::_initialize();
        $this->cModel = new Member();
        $this->bizUserId = $this->cModel->where(['id'=>$request->user_info['id']])->value('bizUserId');
    }

    /**
     * 安全中心首页
     * @return json $data
     * @author kangyi(997032813@qq.com)
     */
    public function index(Request $request)
    {
        $mModel = new Member();
        $user_info = $request->user_info;
        $uid = $user_info['id'];
        if(empty($user_info)){
            outPutData('','未查询到会员信息',Config::get('code.tipsCode'));
        }
        $data = $mModel->field('mobile,certificate,yunmobile')->where(array('id'=>$uid))->find();
        $data['certificate']=substr_replace($data['certificate'],'**********',4,10);
        outPutData($data);
    }

    /**
     * 发送解绑手机号短信
     * @author hey_heyang(635870453@qq.com)
     */
    public function SendUnbindMobileCode(Request $request){
        if(request()->isPost()){
            $user_info = $request->user_info;
            $mModel = new Member();
            $my_data = $mModel->where(array('id'=>$user_info['id']))->field('bizUserId')->find()->toArray();
            $YunClass = new Yunst();
            $mobile = input('post.mobile');
            if(empty($mobile)){
                outPutData('','请填写手机号',Config::get('code.error'));
            }
            $res = $YunClass->sendVerifCode($my_data['bizUserId'],$mobile,6);
            if($res['status'] == 'OK'){
                outPutData('','发送成功',Config::get('code.success'));
            }else{
                outPutData('',$res['message'],Config::get('code.error'));
            }
        }
    }


    /**
     *解绑手机
     * @author hey_heyang(635870453@qq.com)
     */
    public function unBindMobile(Request $request){
        if(request()->isPost()){
            $user_info = $request->user_info;
            $mModel = new Member();
            $YunClass = new Yunst();
            $mobile = input('post.mobile');
            $code = input('post.code');
            if($mobile){
                $usermobile = $mModel->where(['mobile'=>$mobile,'id'=>['neq',$user_info['id']]])->value('mobile');
                if(!empty($usermobile)){
                    outPutData('','该手机已被其他用户作登录账号使用',Config::get('code.error'));
                }
            }
            $my_data = $mModel->where(array('id'=>$user_info['id']))->field('bizUserId')->find()->toArray();
            $res = $YunClass->unBindMobile($my_data['bizUserId'],$mobile,$code);
            if($res['status'] == 'OK'){
                $signedValue=json_decode($res['signedValue'],true);
                if($signedValue['result']=='OK'){
                    $mModel->where(['id'=>$user_info['id']])->update(['yunmobile'=>null,'safeMobile'=>0]);
                    outPutData('','验证成功',Config::get('code.success'));
                }else{
                    outPutData('','验证失败',Config::get('code.error'));
                }

            }else{
                outPutData('',$res['message'],Config::get('code.error'));
            }
        }
    }


    /**
     * 发送云商通短信
     * @author kangyi(997032813@qq.com)
     */
    public function sendYsCode(Request $request){
        $user_info = $request->user_info;
        if(request()->isPost()){
            $YunClass = new Yunst();
            $mobile2 = input('post.newmobile');
            if(empty($mobile2)){
                outPutData('','请填写新手机号',Config::get('code.error'));
            }
            $mModel = new Member();
            $my_data = $mModel->where(array('id'=>$user_info['id']))->field('bizUserId')->find()->toArray();
            $res = $YunClass->sendVerifCode($my_data['bizUserId'],$mobile2);
            if($res['status'] == 'OK'){
                outPutData('','发送成功',Config::get('code.success'));
            }else{
                outPutData('',$res['message'],Config::get('code.error'));
            }
        }
    }

    /**
     * 更绑实名认证手机
     * @author kangyi(997032813@qq.com)
     */
    public function bindMobile(Request $request){
        $user_info = $request->user_info;
        if($request->isPost()){
            $YunClass = new Yunst();
            $mobile2 = input('post.newmobile');
            $code = input('post.code');
            if($mobile2){
                $usermobile = $this->cModel->where(['mobile'=>$mobile2,'id'=>['neq',$user_info['id']]])->value('mobile');
                if(!empty($usermobile)){
                    outPutData('','该手机已被其他用户作登录账号使用',Config::get('code.error'));
                }
                $yunmobile = $this->cModel->where(['yunmobile'=>$mobile2])->value('yunmobile');
                if(!empty($yunmobile)){
                    outPutData('','该手机已被认证',Config::get('code.error'));
                }
            }
            $my_data = $this->cModel->where(array('id'=>$user_info['id']))->field('bizUserId,yunmobile')->find()->toArray();
            $oldMobile = $my_data['yunmobile'];
            if($oldMobile){
                $res = $YunClass->bindMobile2($my_data['bizUserId'],$oldMobile,$mobile2,$code);
            }else {
                $res = $YunClass->bindMobile($my_data['bizUserId'],$mobile2,$code);
            }

            if($res['status'] == 'OK'){
                $this->cModel->where(['id'=>$user_info['id']])->update(['safeMobile'=>1,'yunmobile'=>$mobile2]);
                outPutData('','更绑成功',Config::get('code.success'));
            }else{
                outPutData('',$res['message'],Config::get('code.error'));
            }
        }
    }


    /**
     * 发送登陆手机修改验证码
     * @author kangyi(997032813@qq.com)
     */
    public function sendCode(){
        if(request()->isPost()){
            $mobile = input('post.mobile');
            $code = rand(1000,9999);
            $res = SmsSend($mobile,$code);
            if($res['code'] == 0){
                session('mobilecode',$code);
                session('mobile',$mobile);
                outPutData('','发送成功',Config::get('code.success'));
            }else{
                outPutData('',$res['detail'],Config::get('code.error'));
            }
        }
    }

    /**
     * 验证登陆手机验证码
     * @author kangyi(997032813@qq.com)
     */
    public function verificationCode(){
        outPutData('','修改登录手机号功能已关闭，请联系客服!',Config::get('code.error'));
        if(request()->isPost()){
            $code=input('post.code');
            $strlen=mb_strlen($code,'UTF8');
            if($strlen!=4){
                outPutData('','手机验证码不正确',Config::get('code.codeFail'));
            }
            $newmobile=input('post.mobile');
            if($code != session('mobilecode')){
                outPutData('','手机验证码不正确',Config::get('code.codeFail'));
            }
            if($newmobile != session('mobile')){
                outPutData('','绑定手机与收取验证码手机不一致',Config::get('code.error'));
            } else {
                outPutData('','验证成功',Config::get('code.success'));
            }
        }
    }

    /**
     * 修改登陆手机
     * @author kangyi(997032813@qq.com)
     */
    public function updateMember(Request $request){
        outPutData('','修改登录手机号功能已关闭，请联系客服!',Config::get('code.error'));
        if(request()->isPost()){
            $Mobilelog=new Mobilelog();
            $code=input('post.code');
            $strlen=mb_strlen($code,'UTF8');
            if($strlen!=4){
                outPutData('','手机验证码不正确',Config::get('code.codeFail'));
            }
            $newmobile=input('post.mobile');
            if($code != session('mobilecode')){
                outPutData('','手机验证码不正确',Config::get('code.codeFail'));
            }
            if($newmobile != session('mobile')){
                outPutData('','绑定手机与收取验证码手机不一致',Config::get('code.error'));
            }
            $user_info = $request->user_info;
            $uid = $user_info['id'];
            $oldmobie=$this->cModel->where(['id'=>$uid])->value('mobile');
            $data = [
                'oldmobile' => $oldmobie,
                'newmobile' => $newmobile,
                'uid' => $uid,
                'time' => date('Y-m-d H:i:s')
            ];
            $old=$Mobilelog->where(['oldmobile'=>$newmobile])->find();
            if(!empty($old)){
                outPutData('','该手机号已被系统注销,无法再次作为登录账号',Config::get('code.error'));
            }
            $memold=$this->cModel->where(['mobile'=>$newmobile])->find();
            if(!empty($memold)){
                outPutData('','该手机号已存在',Config::get('code.error'));
            }
            $res=$Mobilelog->insert($data);
            if($res){
                $rut=$this->cModel->where(['id'=>$uid])->update(['mobile'=>$newmobile]);
                if($rut){
                    outPutData('','修改成功',Config::get('code.success'));
                }
                outPutData('','修改失败',Config::get('code.error'));
            }
        }
    }

    /**
     * 忘记登陆账号
     * @author kangyi(997032813@qq.com)
     */
    public function findmobile(Request $request){
        outPutData('','修改登录手机号功能已关闭，请联系客服!',Config::get('code.error'));
        if(request()->isPost()){
            $user_info = $request->user_info;
            $uid = $user_info['id'];
            $Mobilelog=new Mobilelog();
            $code=input('post.code');
            if($code != session('mobilecode')){
                outPutData('','手机验证码不正确',Config::get('code.error'));
            }
            $strlen=mb_strlen($code,'UTF8');
            if($strlen!=4){
                outPutData('','手机验证码不正确',Config::get('code.error'));
            }
            $mobile=input('post.mobile');
            $idcard=input('post.card');
            $name=input('post.username');
            $userdata=$this->cModel->where(['id'=>$uid])->find()->toArray();

            if($userdata['username']!=trim($name)){
                outPutData('','你输入的身份信息非本人信息,请核实.',Config::get('code.error'));
            }elseif ($userdata['certificate']!=trim($idcard)){
                 outPutData('','你输入的身份信息非本人信息,请核实.',Config::get('code.error'));
            }
            $data = [
                'oldmobile' => $userdata['mobile'],
                'newmobile' => $mobile,
                'uid' => $uid,
                'time' => date('Y-m-d H:i:s')
            ];

            $memold=$this->cModel->where(['mobile'=>$mobile])->find();
            if(!empty($memold)){
                outPutData('','该手机号已存在',Config::get('code.error'));
            }
            $old=$Mobilelog->where(['oldmobile'=>$mobile])->find();
            if(!empty($old)){
                outPutData('','该手机号已被系统注销,无法再次作为登录账号',Config::get('code.error'));
            }
            $semobile=session('mobile');
            if($semobile!=$mobile){
                outPutData('','该手机号与收验证码手机不一致,无法再次作为登录账号',Config::get('code.error'));
            }
            $res=$Mobilelog->insert($data);
            if($res){
                $rut=$this->cModel->where(['id'=>$uid])->update(['mobile'=>$mobile]);
                if($rut){
                    outPutData('','修改成功',Config::get('code.success'));
                }
                outPutData('','修改失败',Config::get('code.error'));
            }
        }
    }

    /**
     * 获取支付密码验证码
     * @author kangyi(997032813@qq.com)
     */
    public function getPayCode(Request $request){
        if(request()->isPost()){
            if (Session::has('code')) {
                $code = Session::get('code');
                if (strtolower($code) == strtolower(trim($code))) {
                    $data = $this->cModel->where(['id'=>$request->user_info['id']])->find();
                    if($data['safeMobile'] == 0){
                        outPutData('','请先认证手机',Config::get('code.error'));
                    }
                    $code = rand(1000,9999);
                    $res = SmsSend($data['yunmobile'],$code);
                    if($res['code'] == 0){
                        session('paycode',$code);
                        outPutData('','发送成功',Config::get('code.success'));
                    }else{
                        outPutData('',$res['detail'],Config::get('code.error'));
                    }
                } else {
                    outPutData('','图形验证码不正确',Config::get('code.error'));
                }
            } else {
                outPutData('','验证码不存在，请重新获取验证码',Config::get('code.error'));
            }


        }
    }

    /**
     * 重置支付密码
     * @author kangyi(997032813@qq.com)
     */
    public function setPayPass(Request $request){
        if(request()->isPost()){
            $pass = input('post.pwd');
            $code = input('post.code');
            $strlen=mb_strlen($code,'UTF8');
            if($strlen!=4){
                outPutData('','验证码不正确',Config::get('code.codeFail'));
            }
            if(session('paycode') != $code){
                outPutData('','验证码不正确',Config::get('code.codeFail'));
            }

            $res = $this->cModel->where(['id'=>$request->user_info['id']])->update(['pay_password'=>password_hash($pass,PASSWORD_DEFAULT)]);
            if($res){
                outPutData('','设置成功',Config::get('code.success'));
            }else{
                outPutData('',$this->cModel->getError(),Config::get('code.error'));
            }
        }
    }


    /**
     * 修改登陆密码
     * @author kangyi(997032813@qq.com)
     */
    public function logPass(Request $request){
        if(request()->isPost()){
            $data = input('post.');
            $where = ['id'=>$request->user_info['id']];
            $oldpass = $this->cModel->where($where)->value('password');
            if($oldpass != md5($data['oldpass']) && $oldpass != md5(md5($data['oldpass'])) && !password_verify($data['oldpass'],$oldpass)){
                outPutData('','旧密码不正确',Config::get('code.error'));
            }
            if($oldpass == md5($data['newpass'])){
                outPutData('','新密码与旧密码不能相同',Config::get('code.error'));
            }
            $result = $this->cModel->where($where)->update(['password'=>md5($data['newpass'])]);
            if($result){
                Session::delete('user_id');
                outPutData('','修改成功',Config::get('code.success'));
            }else{
                outPutData('',$this->cModel->getError(),Config::get('code.error'));
            }
        }
    }


}
